package com.paradogs.gs.modules;

import com.paradogs.common.utils.SpringUtils;
import com.paradogs.gs.misc.IManager;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @author: yumi
 * @date: 2024/10/15  19:18
 * @Description: TODO
 */
@Slf4j
@Getter
@Setter
public abstract class GameObject {

    private Long id;

    private Map<Class<? extends IManager>, IManager> mgrMap = new LinkedHashMap<>();

    public GameObject(Long id) {
        this.id = id;
        // 加载 Mgrs
        Class<?> mgrClass = null;
        for (Field field : this.getClass().getDeclaredFields()) {
            try {
                mgrClass = field.getType();
                if (IManager.class.isAssignableFrom(mgrClass)
                        /* && mgrClass.getAnnotation(GameManager.class) != null */) {
                    IManager mgr = (IManager) SpringUtils.getBean(mgrClass);
                    mgr.loadData(this);
                    field.setAccessible(true);
                    field.set(this, mgr);
                    mgrMap.put((Class<? extends IManager>) mgrClass, mgr);
//                    while (mgrClass != null && IManager.class.isAssignableFrom(mgrClass)) {
//                        mgrMap.put((Class<? extends IManager>) mgrClass, mgr);
//                        mgrClass = mgrClass.getSuperclass();
//                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(String.format("Mgr[%s] load fail: %s", mgrClass.getSimpleName(), e.getMessage()), e);
            }
        }

    }

    public Collection<IManager> getMgrs() {
        return mgrMap.values();
    }
    public <T> T getMgr(Class<T> clz) {
        return (T) mgrMap.get(clz);
    }

    public void StartBefore() {}
    public void Start() {}
    public void Destroy() {}
    public void Update() {}

    // 是否需要网络请求
    public boolean isNet() {
        return true;
    }

}
